<?php
$id = $_GET['Id'];
$user_id = $_SESSION['uid'];
$cmpy_nickname = $_SESSION['cname'];
$full_name = $_SESSION['first_name'].' '.$_SESSION['last_name'];
$current_date = date('Y-m-d');

$billperiod_query = "select * from tbl_period";
$billResult = mysql_query($billperiod_query);

$getdcsp_bb_query = "select * from tbl_dcsp_bb where p_id=$id";
$dcsp_bb_result = mysql_num_rows(mysql_query($getdcsp_bb_query));

$getEnddate_query ="select p_end_date from tbl_period where p_id=$id";
$Enddatedata = mysql_fetch_object(mysql_query($getEnddate_query));
$enddate = $Enddatedata->p_end_date;

if($_POST['submit']){
	extract($_POST);
	if($dcsp_bb_result == 0){
	$insert_query = "INSERT INTO tbl_dcsp_bb (p_id,rp_id,c_nickname,dcsp_bb_Submit_date,dcsp_bb_conversation_minutes,dcsp_bb_adjustment) " .
			"values ('$p_id','$rp_id','$c_nickname','$dcsp_bb_Submit_date','$dcsp_bb_conversation_minutes','$dcsp_bb_adjustment')";
	if(mysql_query($insert_query)){?>
		<script type="text/javascript">
		$(document).ready(function(){
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
						$( "#dialog-message" ).dialog({
						modal: true,
						buttons: {
							Ok: function() {
								$(this).dialog( "close" );
								window.location = "index.php?page=home";
							}
						}
					});
			});
		</script>
	<? }
	}else{
		$update_query = "UPDATE tbl_dcsp_bb SET p_id='$p_id',c_nickname='$c_nickname',dcsp_bb_conversation_minutes='$dcsp_bb_conversation_minutes' WHERE p_id =$id";
		if(mysql_query($update_query)){?>
		<script type="text/javascript">
		$(document).ready(function(){
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
				$( "#dialog-message_u" ).dialog({
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog( "close" );
						window.location = "index.php?page=home";
					}
				}
				});
			});
		</script>
<?}	
}}?>
<script type="text/javascript">
$(document).ready(function(){
	$('#conversation').focusout(function() {
	  var convmins = $(".conv_min").val();
	  var totalconv = convmins*2;
	  
	  $("#convres").text(totalconv).show();
	  $('#hidden_total').append($('input#dcsp_bb_conversation_minutes').val(totalconv));
		
	});
});
</script>
<!-- ui-dialog -->
<div id="dialog-message" title="Success Update" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The Billing Period Filled Successfully.
	</p>
</div>
<div id="dialog-message_u" title="Success Update" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The Billing Period has been Updated Successfully.
	</p>
</div>

<fieldset style="color:#000000;font-size:12px;">
<legend><strong>Billing Form</strong></legend>
<form action="" method="post">
<table cellpadding="4">
	<tr>
		<td><label>Monthly Reporting Period</label></td>
		<td>
		<select name="p_id">
		<? while($endDates = mysql_fetch_object($billResult)){?>
			<option value="<?=$endDates->p_id;?>" <? if($endDates->p_end_date==$enddate){echo 'selected';}?>
			><?=$endDates->p_end_date;?></option>
		<?}?>
		</select>
	</tr>
	<tr>
		<td><label>Submitted By</label></td>
		<td><label><?=$full_name;?> (cmpy: <?=$cmpy_nickname;?>)</label>
			<input type="hidden" name="rp_id" value="<?=$user_id;?>" />
		</td>
	</tr>
	<tr>
		<td><label>Interexchange Carrier</label></td>
		<td>
		<select name="c_nickname">
			<option value="Qwest">Qwest</option>
			<option value="ATT">ATT</option>
		</select>
		</td>
	</tr>
	<tr>
		<td><label>Date Submitted</label></td>
		<td><label><?=$current_date;?></label>
		<input type="hidden" name="dcsp_bb_Submit_date" value="<?=$current_date;?>" readonly/>
		</td>
	</tr>
	<tr>
		<td colspan="5">
			<input type="checkbox" name="dcsp_bb_adjustment" value="True" /> Check box if this is an adjustment and enter the adjustment amount, not the new total.
		</td>
	</tr>
	<tr>
		<td>
			<span>Conversation Minutes</span></br>
			<input type="text" name="conversation_minutes" id="conversation" class="number conv_min"/> * 2=
		</td>
		<td>
		<input type="hidden" name="dcsp_bb_conversation_minutes" value="" id="dcsp_bb_conversation_minutes"/></br><span></span>
		<b><span id="convres"></span></b></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td>
			<input type="submit" name="submit" value="Submit" />
		</td>
	</tr>
</table>
</form>
</fieldset>